package com.next.androidintentlibrary;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.utils.net.Uri;

public class MapIntents {
    private Context context;
    private Intent intent;

    private MapIntents(Context context)
    {
        this.context = context;
    }

    public static MapIntents from(Context context)
    {
        return new MapIntents(context);
    }

    public MapIntents locationOf(String address, String placeTitle)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("geo:0,0?q=");

        String addressEncoded = Uri.encode(address);
        sb.append(addressEncoded);

        // pass text for the info window
        String titleEncoded = Uri.encode(" (" + placeTitle + ")");
        sb.append(titleEncoded);

        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse(sb.toString()));
        return this;
    }

    public MapIntents locationOf(float latitude, float longitude)
    {
        return locationOf(latitude, longitude, null);
    }

    public MapIntents locationOf(float latitude, float longitude, String placeName)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("geo:");
        sb.append(latitude);
        sb.append(",");
        sb.append(longitude);

        if (!placeName.isEmpty())
        {
            sb.append("?q=");
            sb.append(latitude);
            sb.append(",");
            sb.append(longitude);
            sb.append("(");
            sb.append(Uri.encode(placeName));
            sb.append(")");
        }

        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse(sb.toString()));
        return this;
    }

    public MapIntents navigateTo(String address)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("google.navigation:q=");
        String addressEncoded = Uri.encode(address);
        sb.append(addressEncoded);
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse(sb.toString()));
        return this;
    }

    public MapIntents navigateTo(float latitude, float longitude)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("google.navigation:q=");
        sb.append(latitude);
        sb.append(",");
        sb.append(longitude);
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse(sb.toString()));
        return this;
    }

    public MapIntents streetViewOf(float latitude, float longitude)
    {
        return streetViewOf(latitude, longitude, null, null, null, null);
    }

    public MapIntents streetViewOf(float latitude, float longitude, float zoom)
    {
        return streetViewOf(latitude, longitude, null, null, zoom, null);
    }

    public MapIntents streetViewOf(float latitude, float longitude, float zoom, int mapZoom)
    {
        return streetViewOf(latitude, longitude, null, null, zoom, mapZoom);
    }

    // TODO:
    private MapIntents streetViewOf(float latitude, float longitude, Float yaw, Integer pitch, Float zoom, Integer mapZoom)
    {
        StringBuilder builder = new StringBuilder("google.streetview:cbll=").append(latitude).append(",").append(longitude);
        String cbpParam = String.format("%s,,%s,%s",
                yaw == null ? "" : yaw,
                pitch == null ? "" : pitch,
                zoom == null ? "" : zoom);
        builder.append("&cbp=1,").append(cbpParam);
        builder.append("&mz=").append(mapZoom);
        intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setUri(Uri.parse(builder.toString()));
        return this;
    }

    public MapIntents showLocationServices()
    {
        intent = new Intent();
        intent.setAction("android.settings.LOCATION_SOURCE_SETTINGS");
        intent.addFlags( 1073741824| 524288);
        //Intent.FLAG_ACTIVITY_NO_HISTORY  = 1073741824
        //Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET = 524288
        return this;
    }

    public Intent build()
    {
        return intent;
    }

    private void startAbility(Intent intent)
    {
        if (!(context instanceof Ability))
        {
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
        }
        context.startAbility(intent,0);
    }

    public void show()
    {
        startAbility(build());
    }
}
